<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <style>
        .container {
            width: 400px;
            margin: 100px auto;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .error-message {
            color: red;
            margin-top: 10px;
            display: none;
        }
        .login-link {
            text-align: center;
            margin-top: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2 style="text-align: center;">用户注册</h2>
        <div class="form-group">
            <label>用户名</label>
            <input type="text" id="username" placeholder="请输入用户名">
        </div>
        <div class="form-group">
            <label>密码</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div class="form-group">
            <label>确认密码</label>
            <input type="password" id="confirmPassword" placeholder="请再次输入密码">
        </div>
        <button onclick="register()">注册</button>
        <div class="error-message" id="errorMessage"></div>
        <div class="login-link">
            已有账号？<a href="login.html">立即登录</a>
        </div>
    </div>

    <script>
        window.onload = function() {
            // 检查是否已登录
            const user = JSON.parse(localStorage.getItem('user'));
            if (user) {
                window.location.href = '/index.html';
            }
        }

        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        function hideError() {
            document.getElementById('errorMessage').style.display = 'none';
        }

        function register() {
            hideError();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            
            if (!username) {
                showError('请输入用户名');
                return;
            }
            
            if (!password) {
                showError('请输入密码');
                return;
            }
            
            if (!confirmPassword) {
                showError('请确认密码');
                return;
            }
            
            if (password !== confirmPassword) {
                showError('两次输入的密码不一致');
                return;
            }

            // 发送注册请求
            fetch('/auth/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    confirmPassword: confirmPassword
                })
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 200) {
                    alert('注册成功！');
                    window.location.href = 'login.html';
                } else {
                    showError(result.msg || '注册失败');
                }
            })
            .catch(error => {
                showError('服务器错误，请稍后重试');
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html> 